package com.xinzhidi.web.custommodel.controller;

import com.xinzhidi.common.annotation.Log;
import com.xinzhidi.common.core.controller.BaseController;
import com.xinzhidi.common.core.domain.AjaxResult;
import com.xinzhidi.common.core.domain.R;
import com.xinzhidi.common.core.page.TableDataInfo;
import com.xinzhidi.common.enums.BusinessType;
import com.xinzhidi.common.utils.poi.ExcelUtil;
import com.xinzhidi.web.custommodel.dto.CustomModelAddDto;
import com.xinzhidi.web.custommodel.dto.CustomModelBatchAddDto;
import com.xinzhidi.web.custommodel.dto.CustomModelListDto;
import com.xinzhidi.web.custommodel.dto.CustomModelUpdDto;
import com.xinzhidi.web.custommodel.service.CustomModelService;
import com.xinzhidi.web.custommodel.vo.CustomModelInfoVo;
import com.xinzhidi.web.custommodel.vo.CustomModelListVo;
import com.xinzhidi.web.plot.domain.vo.SelectVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * 自定义模型Controller
 *
 * @author cx
 * @date 2023-06-15
 */
@RestController
@Api(tags = "自定义模型")
@RequestMapping("/custommodel")
@RequiredArgsConstructor
public class CustomModelController extends BaseController {

    private final CustomModelService customModelService;

    @ApiOperation("查询自定义模型列表")
    @PreAuthorize("@ss.hasPermi('custommodel:custommodel:list')")
    @PostMapping("/listCustomModel")
    public TableDataInfo<CustomModelListVo> listCustomModel(@Validated @RequestBody CustomModelListDto dto) {
        startPagePost(dto);
        return customModelService.selectCustomModelList(dto);
    }

    @ApiOperation("查询自定义模型下拉列表")
    @PostMapping("/listCustomModelSelect")
    public TableDataInfo<SelectVo> listCustomModelSelect(@Validated @RequestBody CustomModelListDto dto) {
        dto.setPageSize(9999);
        startPagePost(dto);
        return customModelService.listCustomModelSelect(dto);
    }

    @ApiOperation("导出自定义模型列表")
    @PreAuthorize("@ss.hasPermi('custommodel:custommodel:export')")
    @Log(title = "自定义模型", businessType = BusinessType.EXPORT)
    @PostMapping("/exportCustomModel")
    public void exportCustomModel(HttpServletResponse response, CustomModelListDto dto) {
        TableDataInfo<CustomModelListVo> list = customModelService.selectCustomModelList(dto);
        ExcelUtil<CustomModelListVo> util = new ExcelUtil<CustomModelListVo>(CustomModelListVo.class);
        util.exportExcel(response, list.getRows(), "自定义模型数据");
    }

    @ApiOperation("获取自定义模型详细信息")
    @PreAuthorize("@ss.hasPermi('custommodel:custommodel:query')")
    @GetMapping(value = "/getCustomModelById")
    public R<CustomModelInfoVo> getCustomModelById(@RequestParam("id") Long id) {
        return R.ok(customModelService.selectCustomModelById(id));
    }

    @ApiOperation("新增自定义模型")
    @PreAuthorize("@ss.hasPermi('custommodel:custommodel:add')")
    @Log(title = "自定义模型", businessType = BusinessType.INSERT)
    @PostMapping("addCustomModel")
    public AjaxResult addCustomModel(@Validated @RequestBody CustomModelAddDto dto) {
        return toAjax(customModelService.insertCustomModel(dto));
    }

    @ApiOperation("修改自定义模型")
    @PreAuthorize("@ss.hasPermi('custommodel:custommodel:edit')")
    @Log(title = "自定义模型", businessType = BusinessType.UPDATE)
    @PostMapping("editCustomModel")
    public AjaxResult editCustomModel(@Validated @RequestBody CustomModelUpdDto dto) {
        return toAjax(customModelService.updateCustomModel(dto));
    }

    @ApiOperation("删除自定义模型")
    @PreAuthorize("@ss.hasPermi('custommodel:custommodel:remove')")
    @Log(title = "自定义模型", businessType = BusinessType.DELETE)
    @DeleteMapping("/delCustomModel")
    public AjaxResult delCustomModel(@RequestParam Long[] ids) {
        return toAjax(customModelService.deleteCustomModelByIds(ids));
    }

    @ApiOperation("批量绑定模型")
    @PostMapping("/batchBindModel")
    public AjaxResult batchBindModel(@Validated @RequestBody CustomModelBatchAddDto dto) {
        return toAjax(customModelService.batchBindModel(dto));
    }

}
